import requests
import re
import pathlib
import json
import pandas as pd
from bs4 import BeautifulSoup
import config.globalVariable as Gv

def get_lhb_data():
    list = get_data()
    plate_data_all = data_conver_from(list)
    return plate_data_all

def set_data(list):
    if list is None or len(list)<3:
        return
    id = list[0]
    name = list[1]
    code = list[2]
    lhbdatafileaddr = pathlib.Path(__file__).parent.parent.parent.resolve().as_posix() + '/' +  Gv.lhbdatafileaddr
    jsondata = {}
    try:
        with open(lhbdatafileaddr, 'r') as f:
            jsondata = json.load(f)
    except (FileNotFoundError, json.JSONDecodeError) as e:
        print(f"Error reading JSON file: {e}")
    data = {}
    jsonList = []
    if jsondata is not None and len(jsondata)>0:
        data = jsondata.get(id)
        if data is not None and len(data)>0:
            jsonList = data.get('list')
        else:
            data={}
    jsonList.append(code)
    data['name'] = name
    data['list'] = jsonList
    jsondata[id] = data

    try:
        with open(lhbdatafileaddr, 'w') as f:
            json.dump(jsondata, f)
    except (FileNotFoundError, json.JSONDecodeError) as e:
        print(f"Error reading JSON file: {e}")


def data_conver_from(list):
    '''每日复盘'''
    if list is None:
        return
    dict = {0: '上榜日期',1: '股票代码', 2: '股票名称', 3: '上榜原因',4:'涨跌幅(%)',5:'买入额(万)',6:'卖出额(万)',7:'买卖净额(万)',8:'所属板块',9:'营业部名称'}
    plate_data_all = pd.DataFrame()
    # 遍历股票列表，提取数据
    for item in list:
        item_data = [
            item[0],item[1],item[2],
            item[3], item[4], item[5],
            item[6], item[7], item[8],item[9]
        ]
        # 将数据转换成DataFrame类型
        data = pd.DataFrame(item_data).T
        data.rename(columns=dict, inplace=True)
        plate_data_all = plate_data_all._append(data, ignore_index=True)
    # 返回DataFrame类型数据
    return plate_data_all

def get_data():
    # list = [{'code':'aab95c82242d819','name':'湘财证券股份有限公司杭州五星路证券营业部'},
    #         {'code':'ef3f5e8e97591ea0','name':'东吴证券股份有限公司苏州干将东路证券营业部'},
    #         {'code':'b08752b7e6b5d212','name':'东海证券股份有限公司南京洪武北路证券营业部'},
    #         {'code':'ec7826ed8df56f33','name':'国盛证券有限责任公司慈溪慈甬路证券营业部'},
    #         {'code':'9133b5ba2dbe7769','name':'国投证券股份有限公司西安曲江池南路证券营业部'},
    #         {'code':'43e7239480d2ee1d','name':'东方证券股份有限公司厦门仙岳路证券营业部'},
    #         {'code': 'd1a4f17483fd8ef7', 'name': '东方证券股份有限公司杭州龙井路证券营业部'},
    #         {'code': '17d8601ce8bec03a', 'name': '海通证券股份有限公司济南舜华路证券营业部'},
    #         {'code': 'e37de6781d32bcb6', 'name': '安信证券股份有限公司佛山汾江南路证券营业部'},
    #         {'code': '35c33e093134fec6', 'name': '中泰证券股份有限公司济南经十路证券营业部'},
    #         {'code': 'feb504bee69375a9', 'name': '华宝证券股份有限公司深圳新闻路证券营业部'},
    #         {'code': 'c21b62813c42cdc6', 'name': '华宝证券股份有限公司上海东大名路证券营业部'},
    #         {'code': 'b2786171dc1fd2df', 'name': '天风证券股份有限公司成都聚龙路证券营业部'},
    #         {'code':'56160c92a2a846b2','name':'长江证券股份有限公司昆明白龙路证券营业部'},
    #         {'code': '21db34c5e04c6506', 'name': '中信证券股份有限公司大庆纬十一路证券营业部'},
    #         {'code': '521f110cca4061b8', 'name': '海通证券股份有限公司汕头中山中路证券营业部'},
    #         {'code': '27273861418638c5', 'name': '安信证券股份有限公司汕头长平路证券营业部'}]
    lhbdatafileaddr = pathlib.Path(__file__).parent.parent.parent.resolve().as_posix() + '/' + Gv.lhbdatafileaddr
    jsondata = {}
    try:
        with open(lhbdatafileaddr, 'r') as f:
            jsondata = json.load(f)
    except (FileNotFoundError, json.JSONDecodeError) as e:
        print(f"Error reading JSON file: {e}")
    if jsondata is None or len(jsondata)<=0:
        return

    current_date = getDate()
    data_list_all = []
    for key in jsondata:
        date = jsondata[key]
        shortname = date.get('name')
        list = date.get('list')
        data_list_all.append(['',shortname,'','','','','','','',''])
        for code in list:
            # code = item['code']
            # if code is None:
            #     continue
            url = "https://data.10jqka.com.cn/market/lhbyyb/orgcode/"+code+"/"
            payload = {}
            headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15'}
            response = requests.request("GET", url, headers=headers, data=payload)
            if response.status_code != 200:
                return
            # 使用BeautifulSoup解析HTML内容
            soup = BeautifulSoup(response.text, 'lxml')
            name = soup.find('div', class_='page-row').find('div').find('h2').get_text(strip=True)
            # 查找class为'zdph'的div元素
            zdph_div = soup.find('div', class_='zdph')
            # 在找到的div元素中查找table元素
            table = zdph_div.find('table')
            # 在table元素中查找tbody元素
            tbody = table.find('tbody')
            # 遍历tbody中的tr元素，并获取每个tr中的td元素
            for tr in tbody.find_all('tr'):
                date = tr.find('td').get_text(strip=True)
                if current_date != date:
                    continue
                tds = tr.find_all('td')
                data_list = []
                for index,td in enumerate(tds):
                    text = td.get_text(strip=True)
                    if index == 1:
                        a_tag = td.find('a')
                        href = a_tag.get('href')
                        match = re.search(r'/code/(\d+)/date/',href)
                        a_text = a_tag.get_text(strip=True)
                        data_list.append(a_text)
                        text = match.group(1)
                    data_list.append(text)
                if len(data_list)>0:
                    data_list.append(name)
                    data_list_all.append(data_list)
    return data_list_all



def getDate():
    url = "https://data.10jqka.com.cn/market/longhu/"
    payload = {}
    headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15'}
    response = requests.request("GET", url, headers=headers, data=payload)
    if response.status_code != 200:
        return
    soup = BeautifulSoup(response.text, 'lxml')
    m_text_date_input = soup.find('input', class_='m_text_date')
    value = m_text_date_input.get('value')
    return value

if __name__ == '__main__':
    #set_data(['td','腾得','27273861418638c5','海通证券股份有限公司汕头中山中路证券营业部'])
    #print(get_data())

    print(getDate())